package com.edu.whty.common.tools;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * 对List集合进行分页
 *
 * @param
 * @author hefeng
 * @date 2023年06月09日
 */


public class CustomPage<T> {

    //当前页
    private Integer pageNum;
    //总记录数
    private Integer total;
    //总页数
    private Integer pages;
    //当前页显示多少条记录

//    @JsonIgnore
    private Integer size;

    private List<T> list;

    public CustomPage(List<T> list, Integer pageNum, Integer size) {

        this.pageNum = pageNum;
        this.size = size;
        this.total = list.size();

        //总记录数和每页显示的记录之间是否可以凑成整数（pages）
        boolean full = total % size == 0;

        //分页 == 根据size（每页显示的记录数）计算pages
        if (!full) {
            //如果凑不成整数
            this.pages = total / size + 1;
        } else {
            //如果凑成整数
            this.pages = total / size;
        }

        int fromIndex = 0;
        int toIndex = 0;
        fromIndex = pageNum * size - size;
        if (pageNum == 0) {
            throw new ArithmeticException("第0页无法展示");
        } else if (pageNum > pages) {
            //如果查询的页码数大于总的页码数，list设置为[]
            list = new ArrayList<>();
        } else if (pageNum.equals(pages)) {
            //如果查询的当前页等于总页数，直接索引到total处
            toIndex = total;
        } else {
            //如果查询的页码数小于总页数，不用担心切割List的时候toIndex索引会越界，直接等
            toIndex = pageNum * size;
        }

        if (list.size() == 0) {
            this.list = list;
        } else {
            this.list = list.subList(fromIndex, toIndex);
        }

    }


    public Integer getPageNum() {
        return pageNum;
    }


    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }


    public Integer getTotal() {
        return total;
    }


    public void setTotal(Integer total) {
        this.total = total;
    }


    public Integer getPages() {
        return pages;
    }


    public void setPages(Integer pages) {
        this.pages = pages;
    }


    public Integer getSize() {
        return size;
    }


    public void setSize(Integer size) {
        this.size = size;
    }


    public List<T> getList() {
        return list;
    }


    public void setList(List<T> list) {
        this.list = list;
    }

}
